class utils {
    toHexString(num: number) {
        return num.toString(16);
    }

    isEmpty(obj: any): boolean {
        if (typeof obj == 'undefined' || obj == null || obj?.toString().trim() == '')
            return true;
        return false;
    }

    toRgb(num: number): rgb {
        let r = new rgb();
        r.b = num & 0xff;
        r.g = (num >> 8) & 0xff;
        r.r = (num >> 16) & 0xff;
        return r;
    }

    toHls(s: rgb): hls {
        var rgb = [s.r, s.g, s.b];
        var maxc = Math.max(...rgb);
        var minc = Math.max(...rgb);
        let l = (minc + maxc) / 2.0;
        if (minc == maxc) return new hls(0.0, l, 0.0);
        let s2 = l <= 0.5 ? (maxc - minc) / (maxc + minc) : (maxc - minc) / (2.0 - maxc - minc);
        var rc = (maxc - rgb[0]) / (maxc - minc);
        var gc = (maxc - rgb[1]) / (maxc - minc);
        var bc = (maxc - rgb[2]) / (maxc - minc);
        let h = 0;
        if (rgb[0] == maxc) h = bc - gc;
        if (rgb[1] == maxc) h = 2.0 + rc - bc;
        if (rgb[2] == maxc) h = 4.0 + gc - rc;
        h = h / 6.0 % 1.0;
        return new hls(h * 360, l * 100, s2 * 100);
    }

    formatTime(time: number): string {
        const hour = parseInt((time / 3600).toString());
        const minute = parseInt((time / 60 % 60).toString());
        const second = Math.ceil(time % 60);
        const hours = hour < 10 ? '0' + hour : hour;
        const formatSecond = second > 59 ? 59 : second;
        return `${hour > 0 ? `${hours}:` : ''}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`;
    }

    getShadowColor(color: number): number {
        let red = (color >> 16) & 0xFF
        let green = (color >> 8) & 0xFF
        let blue = color & 0xFF
        let g = red * 0.299 + green * 0.587 + blue * 0.114;
        if (g >= 192)
            return 0x000000;
        else
            return 0xffffff;
    }
}
export default new utils();

export class hls {
    constructor(h: number, l: number, s: number) {
        this.h = h;
        this.l = l;
        this.s = s;
    }
    h: number;
    l: number;
    s: number;
}

export class rgb {
    r: number;
    g: number;
    b: number;
}